/*
 * Copyright 2020 tu.cn All right reserved. This software is the
 * confidential and proprietary information of tu.cn ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Tu.cn
 */
package com.onedec.page.controller;

import com.onedec.entity.OnedecEmp;
import com.onedec.entity.OnedecFriendGroup;
import com.onedec.entity.OnedecMsgGroup;
import com.onedec.page.feign.MessageClient;
import com.onedec.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 31014
 * @date 2020/7/13 15:34
 */
@RestController
@RequestMapping("message")
public class MessageController {
    @Autowired
    private MessageClient messageClient;

    @GetMapping("init")
    public ResponseEntity initMessage(HttpServletRequest httpServletRequest) {
        final com.onedec.admin.entity.OnedecEmp emp =(com.onedec.admin.entity.OnedecEmp) httpServletRequest.getSession().getAttribute("emp");
        final Integer empId = emp.getEmpId();
        final ResponseEntity<OnedecEmp> mine = messageClient.getMine(empId);
        final HashMap<String, Object> mineMap = new HashMap<>();
        mineMap.put("username", mine.getData().getEmpName());
        mineMap.put("id", mine.getData().getEmpId());
        mineMap.put("status", "online");
        mineMap.put("sign", mine.getData().getEmpBak1());
        mineMap.put("avatar", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595245597797&di=69666dd1042f1849f904ea7d4331b495&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201805%2F30%2F20180530083702_HyrTS.thumb.700_0.jpeg");
        final ResponseEntity<List<OnedecMsgGroup>> groups = messageClient.getGroups(empId);
        final ResponseEntity<List<OnedecFriendGroup>> friends = messageClient.getFriends(empId);
        final List<HashMap<String, Object>> friendsMap = new ArrayList<>();
        final List<OnedecFriendGroup> data = friends.getData();
        data.forEach(e -> {
            final HashMap<String, Object> map = new HashMap<>();
            map.put("groupname", e.getFriendGroupName());
            map.put("id", e.getFriendGroupId());
            map.put("list", new ArrayList<Map<String, Object>>());
            final List<Map<String, Object>> eFriends = e.getFriends();
            eFriends.forEach(a -> {
                final HashMap<String, Object> friend = new HashMap<>();
                friend.put("username", a.get("emp_name"));
                friend.put("id", a.get("emp_id"));
                friend.put("avatar", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595245597797&di=69666dd1042f1849f904ea7d4331b495&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201805%2F30%2F20180530083702_HyrTS.thumb.700_0.jpeg");
                friend.put("sign", a.get("emp_bak1"));
                friend.put("status", "online");
                ((ArrayList) (map.get("list"))).add(friend);
            });
            friendsMap.add(map);
        });
        final ArrayList<Map<String, Object>> list = new ArrayList<>();
        groups.getData().forEach(c -> {
            final HashMap<String, Object> group = new HashMap<>();

            group.put("groupname", c.getMsgGroupName());
            group.put("id", c.getMsgGroupId());
            group.put("avatar", "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1595245597797&di=69666dd1042f1849f904ea7d4331b495&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201805%2F30%2F20180530083702_HyrTS.thumb.700_0.jpeg");
            list.add(group);
        });
        final HashMap<Object, Object> init = new HashMap<>();
        init.put("mine", mineMap);
        init.put("friend", friendsMap);
        init.put("group", list);
        return new ResponseEntity("0", "成功", init);


    }
}